<?php

require_once(t3lib_extMgm::extPath('cms').'layout/interfaces/interface.tx_cms_layout_tt_content_drawitemhook.php');

class tx_filelinks_drawItemHook implements tx_cms_layout_tt_content_drawItemHook
{

    /**
     * @param tx_cms_layout $parentObject
     * @param int $drawItem
     * @param string $headerContent
     * @param string $itemContent
     * @param array $row
     */
	public function preProcess(tx_cms_layout &$parentObject, &$drawItem, &$headerContent, &$itemContent, array &$row)
	{
        if($row['CType']=='filelinks_pi1') {
            if(!empty($row['tx_filelinks'])) {

                $drawItem = 1;

                $where  = 'parentid='.intval($row['uid']);
                $where .= t3lib_BEfunc::BEenableFields('tx_filelinks');
                $where .= t3lib_BEfunc::deleteClause('tx_filelinks');

                $links = $GLOBALS['TYPO3_DB']->exec_SELECTgetRows('*', 'tx_filelinks', $where, '', 'sorting');
                
                if(count($links) > 0) {
                    
                    foreach($links AS $index => $link) {
                        $headerContent .= $this->renderLinkInfo($index, $link);
                    }

                }
            }
        }
	}

    /**
     * @param integer $index
     * @param string $link
     * @return string
     */
    public function renderLinkInfo($index, $link)
    {
        $content = $index+1 . ' ';
        switch($link['link_type']) {
            case 1:
                $content .= 'Internal page: ' . $link['link_page'];
                break;
            case 2:
                $content .= 'Internal file: ' . urldecode(basename($link['link_file']));
                
                $info = array();
                if($link['fileinfo']>0) {
                    $info[] = 'info';
                }
                if($link['secure']>0) {
                    $info[] = 'secure';
                }
                if($link['forcedl']>0) {
                    $info[] = 'force';
                }

                if(!empty($info)) {
                    $content .= ' (' . implode(', ', $info) . ')';
                }

                break;
            case 3:
                $content .= 'External page: ' . $link['link_url'];
                break;
            case 4:
                $content .= 'E-Mail: ' . $link['link_mail'];
                break;
        }
        return $content . '<br />';
    }

}

if (defined('TYPO3_MODE') && $TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/filelinks/class.tx_filelinks_drawItemHook.php'])	{
    include_once($TYPO3_CONF_VARS[TYPO3_MODE]['XCLASS']['ext/filelinks/class.tx_filelinks_drawItemHook.php']);
}
